package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.util.JDBCUtil;
import com.situ.blog.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlInOutParameter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BlogDaoImpl implements IBlogDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Blog> selectByPage(BlogQuery blogQuery) {
        String sql = "SELECT id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time FROM blog WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        String queryTypeId = blogQuery.getTypeId();
        if (queryTypeId != null && !"".equals(queryTypeId)) {
            sql += " AND type_id=?";
            queryList.add(queryTypeId);
        }
        Date beginCreateTime = blogQuery.getBeginCreateTime();
        Date endCreateTime = blogQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql += " LIMIT ?,?";
        int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        queryList.add(offset);
        queryList.add(blogQuery.getLimit());

        List<Blog> list = template.query(sql, new BeanPropertyRowMapper<>(Blog.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(BlogQuery blogQuery) {
        String sql = "SELECT count(*) FROM blog WHERE 1=1";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        String queryTypeId = blogQuery.getTypeId();
        if (queryTypeId != null && !"".equals(queryTypeId)) {
            sql += " AND type_id=?";
            queryList.add(queryTypeId);
        }
        Date beginCreateTime = blogQuery.getBeginCreateTime();
        Date endCreateTime = blogQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }

        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM blog WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    public void add(Blog blog) {
        String sql = "INSERT INTO blog(name,type_id,image,content) VALUES (?,?,?,?)";
        template.update(sql, blog.getName(), blog.getTypeId(), blog.getImage(), blog.getContent());
    }

    @Override
    public Blog selectById(int id) {
        String sql = "SELECT id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time FROM blog WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Blog.class), id);
    }

    @Override
    public void update(Blog blog) {
        String sql = "UPDATE blog SET name=?,type_id=?,image=?,content=? WHERE id=?";
        template.update(sql, blog.getName(), blog.getTypeId(), blog.getImage(), blog.getContent(), blog.getId());
    }

    @Override
    public void updateStatus(String id, int status) {
        String sql = "UPDATE blog SET status=? WHERE id=?";
        template.update(sql, status, id);
    }

    @Override
    public List<Blog> selectAll() {
        String sql = "SELECT id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time FROM blog";
        return template.query(sql, new BeanPropertyRowMapper<>(Blog.class));
    }

    @Override
    public void addExcel(Blog blog) {
        template.update("insert into blog(name,image,click_count,type_id,status,is_deleted,create_time,update_time) values(?,?,?,?,?,?,?,?)",
                blog.getName(), blog.getImage(),blog.getClickCount(),blog.getTypeId(),blog.getStatus(),blog.getIsDeleted(),blog.getCreateTime(), blog.getUpdateTime());
    }
}
